package cn.tanghom.framework.system.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.tanghom.framework.system.dao.OrganizationDao;
import cn.tanghom.framework.system.entity.Organization;

@Repository
public class OrganizationDaoImpl extends BaseDaoImpl<Organization> implements OrganizationDao  {

    @Override
    public List GetParentsIdQueryChildren(String parentsId) {
        String sql="with cte as (select * from s_organization where pid='"+parentsId+"' union all "
                + "select  a.* from s_organization as a,cte as b where a.pid=b.id) select * from cte union all select  c.* from s_organization  c where c.id='"+parentsId+"'";
        return getSession().createSQLQuery(sql).list();
    }

    @Override
    public List GetParentsIdQueryChildrenParentsExsit(String parentsId) {
        String sql="with cte as (select * from s_organization where pid='"+parentsId+"' union all "
                + "select  a.* from s_organization as a,cte as b where a.pid=b.id) select * from cte";
        return getSession().createSQLQuery(sql).list();
    }

    @Override
    public List GetChildrenQueryParentsId(String childId) {
        String sql="select a.* from s_organization a , f_GetChildrenQueryParentsId('"+childId+"') b where a.id = b.id ";
        return getSession().createSQLQuery(sql).list();
    }

    @Override
    public void add(Organization organization) {
       getSession().save(organization);
        
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Organization> findTreeList() {
       String hql=" from Organization o where o.isHide > 0  ORDER BY o.sort asc";
        return getSession().createQuery(hql).list();
    }

    @Override
    public Organization findById(Long Id) {
        return (Organization) getSession().get(Organization.class, Id);
    }

    @Override
    public void delete(Organization organization) {
       getSession().delete(organization);;
    }

	@Override
	public List<Organization> findAll() {
		 String hql=" from Organization o order by o.sort asc";
        return getSession().createQuery(hql).list();
	}

}
